﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.门店出入库.Service;
using DiaoPaiDaYin;
using Seagull.BarTender.Print;
using DXApplicationTangche.UC.下单MTO.Service;

namespace DXApplicationTangche.UC.门店出入库.淘宝
{
    public partial class Form客户地址 : DevExpress.XtraEditors.XtraForm
    {
        private List<TagPrintDTO> tagPrintDTOs = new List<TagPrintDTO>();
        public Form客户地址(List<TagPrintDTO> tagPrintDTOs)
        {
            InitializeComponent();
            this.tagPrintDTOs = tagPrintDTOs;
            initCombobox();
        }
        public Form客户地址()
        {
            InitializeComponent();
            initCombobox();
        }

        private void initCombobox()
        {
            this.comboBox省份.DataSource = null;
            try
            {
                List<AreaDetailDTO> areaDetailDTOs省份 = MTO订单Service.getAreaDetail("7");
                this.comboBox省份.DataSource = areaDetailDTOs省份;
                this.comboBox省份.DisplayMember = "name";
            }
            catch { }
        }

        private void comboBox省份_TextChanged(object sender, EventArgs e)
        {
            this.comboBox城市.DataSource = null;
            this.comboBox区域.DataSource = null;
            try
            {
                AreaDetailDTO dto省份 = this.comboBox省份.SelectedItem as AreaDetailDTO;
                List<AreaDetailDTO> areaDetailDTOs城市 = MTO订单Service.getAreaDetail(dto省份.areaId);
                this.comboBox城市.DataSource = areaDetailDTOs城市;
                this.comboBox城市.DisplayMember = "name";
            }
            catch{ }
        }

        private void comboBox城市_TextChanged(object sender, EventArgs e)
        {
            this.comboBox区域.DataSource = null;
            try
            {
                AreaDetailDTO dto城市 = this.comboBox城市.SelectedItem as AreaDetailDTO;
                List<AreaDetailDTO> areaDetailDTOs县区 = MTO订单Service.getAreaDetail(dto城市.areaId);
                this.comboBox区域.DataSource = areaDetailDTOs县区;
                this.comboBox区域.DisplayMember = "name";
            }
            catch { }
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.textBox收件人.Text) 
                    || String.IsNullOrWhiteSpace(this.textBox电话.Text) 
                        || String.IsNullOrWhiteSpace(this.comboBox省份.Text)
                            || String.IsNullOrWhiteSpace(this.comboBox城市.Text)
                                || String.IsNullOrWhiteSpace(this.comboBox区域.Text)
                                    || String.IsNullOrWhiteSpace(this.richTextBox详细地址.Text)
                                        || String.IsNullOrWhiteSpace(this.textBox邮编.Text)
                                        )
            {
                MessageBox.Show("请填写信息完全");
                return;
            }
            DialogResult dr = MessageBox.Show("确定信息？", "提示:确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                String re = 
                    出入库Service.post生成成衣包装贴(
                        this.textBox收件人.Text.Trim(),
                        this.textBox电话.Text.Trim(),
                        this.comboBox省份.Text,
                        this.comboBox城市.Text,
                        this.comboBox区域.Text,
                        this.richTextBox详细地址.Text.Trim(),
                        this.textBox邮编.Text.Trim(),
                        this.tagPrintDTOs
                        );
                if (!String.IsNullOrEmpty(re))
                {
                    try
                    {
                        //this.print包装贴(re);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("操作失败");
                    return;
                }
            }
            else
            {
                return;
            }
            
        }
        private void print包装贴(String re)
        {
            //String str = System.AppDomain.CurrentDomain.BaseDirectory;
            //新建BarTender并启动，模板位置
            Engine btEngine = new Engine();
            btEngine.Start();
            LabelFormatDocument btFormat = btEngine.Documents.Open("C:\\淘宝包装贴.btw");
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "HPRT HLP106S-UE";
            //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
            btFormat.PrintSetup.PrinterName = "POSTEK G-3106";
            //btFormat.PrintSetup.PrinterName = "PDFCreator";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 2;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 2;
            //对BTW模版相应字段进行赋值 

            btFormat.SubStrings["packageCode"].Value = re;
            btFormat.SubStrings["contact"].Value = this.textBox收件人.Text.Trim();
            btFormat.SubStrings["mobile"].Value = this.textBox电话.Text.Trim();
            btFormat.SubStrings["packageBarcode"].Value = re;

            Messages messages;
            int waitout = 10000; // 10秒 超时 
            Seagull.BarTender.Print.Result nResult1 = btFormat.Print("包装帖" + this.textBox收件人.Text.Trim(), waitout, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改 
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
        }
    }
    //public class DTO淘宝出库
    //{
    //    public String name { get; set; }//收件人
    //    public String phone { get; set; }//收件人
    //    public String province { get; set; }//收件人
    //    public String city { get; set; }//收件人
    //    public String distinct { get; set; }//收件人
    //    public String details { get; set; }//收件人
    //    public String expressNo { get; set; }//收件人
    //    public List<TagPrintDTO> tagPrintDTOs { get; set; } = new List<TagPrintDTO>(); //收件人
    //    public DTO淘宝出库()
    //    {

    //    }

    //    public DTO淘宝出库(
    //        string name, 
    //        string phone, 
    //        string province, 
    //        string city, 
    //        string distinct, 
    //        string details, 
    //        string expressNo, 
    //        List<TagPrintDTO> tagPrintDTOs
    //        )
    //    {
    //        this.name = name;
    //        this.phone = phone;
    //        this.province = province;
    //        this.city = city;
    //        this.distinct = distinct;
    //        this.details = details;
    //        this.expressNo = expressNo;
    //        this.tagPrintDTOs = tagPrintDTOs;
    //    }
    //}
}